package com.kds.smarthome.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.kds.smarthome.annotation.BuildingCode;
import com.kds.smarthome.annotation.Log;
import com.kds.smarthome.common.AjaxResponse;
import com.kds.smarthome.controller.vo.IndoorEnvironment;
import com.kds.smarthome.entity.Building;
import com.kds.smarthome.entity.HvacMonitorCfg;
import com.kds.smarthome.security.jwt.TokenProvider;
import com.kds.smarthome.service.EnvironmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 孔德嵩
 * 创建日期 2024/12/11
 */
@Slf4j
@RestController
@RequestMapping("/api/environment")
public class EnvironmentController {


    @Autowired
    private TokenProvider tokenProvider;
    @Autowired
    private EnvironmentService environmentService;


    @Log(title = "查询室内环境", businessType = 0)
    @GetMapping("/current")
    public AjaxResponse<IndoorEnvironment> indoorEnvironment(@BuildingCode String buildingCode,
                                                             @RequestHeader("Authorization") String token) {
        try {
            Building currentBuilding = tokenProvider.getCurrentBuilding(token, buildingCode);
            if (currentBuilding == null) {
                return AjaxResponse.error("未找到指定建筑");
            }

            IndoorEnvironment environment = environmentService.queryIndoorEnvironmentCurrent(currentBuilding.getCode());

            environment.setRegion(currentBuilding.getName());

            environment.setDate(new Date());

            return AjaxResponse.success(environment);
        } catch (RuntimeException e) {
            return AjaxResponse.error(e.getMessage());
        }
    }

    @Log(title = "查询历史环境数据", businessType = 0)
    @GetMapping("/history")
    public AjaxResponse<List<IndoorEnvironment>> getHistoryIndoorEnvironment(
            @BuildingCode String buildingCode,
            @RequestHeader("Authorization") String token,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {

        try {
            Building currentBuilding = tokenProvider.getCurrentBuilding(token, buildingCode);
            if (currentBuilding == null) {
                return AjaxResponse.error("未找到指定建筑");
            }


            DateTime end = DateUtil.offsetDay(endDate, 1);
            log.info("查询历史环境数据: location={}, start={}, end={}",
                    currentBuilding.getLocation(), startDate, end);

            List<IndoorEnvironment> historyData = environmentService.queryIndoorEnvironmentHistory(buildingCode, startDate, end);


            return AjaxResponse.success(historyData);
        } catch (RuntimeException e) {
            return AjaxResponse.error(e.getMessage());
        }
    }


    @Log(title = "查询暖通配置", businessType = 0)
    @GetMapping("/hvac/cfg")
    public AjaxResponse<HvacMonitorCfg> getHVACCfg(
            @BuildingCode String buildingCode,
            @RequestHeader("Authorization") String token) {

        HvacMonitorCfg hvacMonitorCfg = environmentService.getHvacMonitorCfg(buildingCode);


        return AjaxResponse.success(hvacMonitorCfg);
    }




    @Log(title = "查询当前暖通数据", businessType = 0)
    @PostMapping("/hvac")
    public AjaxResponse< Map<String,Object>> currentHVAC(
            @BuildingCode String buildingCode,
            @RequestHeader("Authorization") String token) {

        Map<String,Object> rs=environmentService.currentHVAC(buildingCode);

        return AjaxResponse.success(rs);
    }



    @Log(title = "查询暖通历史数据", businessType = 0)
    @GetMapping("/hvac/history")
    public AjaxResponse<List<Map<String,Object>>> getHVACHistory(
            @BuildingCode String buildingCode,
            @RequestHeader("Authorization") String token,
            @RequestParam String fid,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {

        DateTime end = DateUtil.offsetDay(endDate, 1);
        List<Map<String,Object>> rs=environmentService.getHVACHistoryByFid(fid,startDate,end);

        return AjaxResponse.success(rs);


    }

}
